$(function () {
    $('#user-list').DataTable({
        processing: true,
        serverSide: true,
        lengthChange: true,//是否允许用户改变表格每页显示的记录数
        orderMulti: true,  //启用多列排序
        ordering: true,//使用排序
        bStateSave: true,//记录cookie
        columnDefs: [{
            // "targets": 'nosort',  //列的样式名
            // "orderable": false    //包含上样式名‘nosort’的禁止排序
            'targets': [0, 3, 4, 5, 6, 7, 8, 9],    //除第六，第七两列外，都默认不排序
            'orderable': false
        }],
        order: [],  //取消默认排序查询,否则复选框一列会出现小箭头
        paging: true,//是否分页
        pagingType: "full_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
        // scrollX: true,//允许水平滚动
        // scrollY: "200px",
        // scrollCollapse: true,
        searching: false,//是否开始本地搜索
        stateSave: false,//刷新时是否保存状态
        autoWidth: true,//自动计算宽度
        deferRender: true,//延迟渲染
        language: {
            "lengthMenu": "每页 _MENU_ 条记录",
            "zeroRecords": " ",
            "info": "当前 _START_ 条到 _END_ 条 共 _TOTAL_ 条",
            "infoEmpty": "无记录",
            "infoFiltered": "(从 _MAX_ 条记录过滤)",
            // "search": "用户",
            "processing": "载入中",
            "paginate": {
                "first": "首页",
                "previous": "上一页",
                "next": "下一页",
                "last": "尾页"
            }
        },
        ajax: {
            url: ctx + '/user/list',
            type: 'POST'
        },
        columns: [
            {data: "", defaultContent: "<input type=\"checkbox\" name=\"\" value=\"\">"},
            {data: "userId"},
            {data: "phone"},
            {data: "name"},
            {data: "jobTitle", defaultContent: ""},
            {data: "currentResidence", defaultContent: ""},
            {data: "userName", defaultContent: ""},
            {data: "password", defaultContent: ""},
            {
                data: "shared", render: function (data) {
                    switch (data) {
                        case '0':
                            return '不愿意';
                        case '1':
                            return '愿意';
                        default:
                            return '未知';
                    }

                }

            },
            {
                data: "操作", class: "center",
                defaultContent: "<input onclick='query_show(this)'  class=\" btn btn-primary radius\" type=\"button\" value=\"查看\"> " +
                    "<input  onclick='edit_show(this)'  class=\" btn radius btn-secondary\" type=\"button\" value=\"修改\">" +
                    "<input onclick='delete_show(this)'  class=\" btn radius btn-warning\"  type=\"button\" value=\"删除\">"
            }
        ]
    });


});

//查询业务
function query_show(t) {
    console.log("query");
    var title = "查看";
    var url = ctx + "/view/user/query";
    //获取父对象tr对象
    var input = $(t);
    var tr = input.parent().parent();
    //获取该tr对象的第二列
    var col = tr.find("td:eq(1)").html();
    url = url + "/" + col;
    layer_show(title, url);
}

//添加业务
function add_show(title, url) {
    layer_show(title, url);
}


//编辑业务
function edit_show(t) {
    console.log("edit_show");
    var title = "修改";
    var url = ctx + "/view/user/edit";
    //获取父对象tr对象
    var input = $(t);
    var tr = input.parent().parent();
    //获取该tr对象的第二列
    var col = tr.find("td:eq(1)").html();
    url = url + "/" + col;
    layer_show(title, url);
}

function delete_show(t) {
    var input = $(t);
    var tr = input.parent().parent();
    var col_id = tr.find("td:eq(1)").html();

    layer.confirm('确认要删除吗？', function () {
        var url = ctx + '/user/delete';
        url = url + "/" + col_id;
        $.ajax({
            type: 'POST',
            url: url,
            dataType: 'json',
            success: function (data) {
                if (data.code === 200) {
                    reDrew();
                    layer.msg('已删除!', {icon: 1, time: 1000});
                } else {
                    layer.msg(res.code, {time: 1000});
                }
            }
        });
    });
}

//重新绘制data tables
function reDrew() {
    $('#user-list').DataTable().draw(false);
}

//批量删除
function delete_all() {
    layer.confirm("你确定要批量删除吗？", function () {
        var target = [];
        var i = 0;
        $('#user-list tr').each(function (i) {
            if (i !== 0) {
                var ch = $(this).find('td:eq(0) input');//取表格的第i行第四列的input标签的值
                if (ch.prop("checked")) {
                    var id = parseInt($(this).find('td:eq(1)').html());
                    target.push(id);
                }

            }
            i++;
        });
        var jsonData = {ids: target};
        jsonData = JSON.stringify(jsonData);

        $.ajax({
            url: ctx + "/user/multiDelete",
            type: "post",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: jsonData,
            success: function (res) {
                if (res.code === 200) {
                    reDrew();
                    layer.msg("成功删除", {icon: 1, time: 1000})
                } else {
                    layer.msg("删除失败", {time: 1000})
                }
            }
        })
    });

}